Developer --> Technical Publications
PATHJava Developer Documentation > Mac OS Runtime for Java > JManager > Programming With JManager


Creating an AWT Context

On the Mac OS, an AWT context is defined by a JMAWTContextRef object. Every Java program running within a session has its own AWT context. You can create an AWT context before or after instantiating a locator for the applet you want to run, but you must instantiate the AWT context before instantiating the applet.

To instantiate a JMAWTContextRef object, you call the JMNewAWTContext function JMNewAWTContext as shown in Listing 1-7. You must have instantiated a session already before creating an AWT context.

Listing 1-7 Creating an AWT context

/* define callbacks for the AWT context */
JMAWTContextCallbacks sessionCallbacks = {
    kJMVersion,         /* should be kJMVersion */
    MyRequestFrame,     /* callback to create a frame */
    MyReleaseFrame,     /* callback to release a frame */
    MyUniqueMenuID,     /* callback to give the AWT a valid MenuID */
    MyExceptionOccurred,/* notify that an exception occurred */
    };

/* create an AWT context for this applet */
JMAWTContextRef context;
err = JMNewAWTContext(&context, theSession, &sessionCallbacks, 0);

The value context references the JMAWTContext object, and you should pass this value in other JManager functions to specify this particular context.

You must specify a number of callbacks when calling JMNewAWTContext . JManager uses these callbacks to handle requests from the Java program for new frames (that is, windows). The MyRequestFrame callback MyRequestFrame creates a new window, MyReleaseFrame releases a window MyReleaseFrame, and MyUniqueMenuID creates a new menu ID MyUniqueMenuID. For example, if the Java program requests that a frame be made available, the application-defined callback function MyRequestFrame should request a new Mac OS window. Listing 1-8 shows an example of such a function.

Listing 1-8 Application-defined new frame function

OSStatus MyRequestFrame(JMAWTContextRef context, JMFrameRef newFrame,
    JMFrameKind kind, Rect bounds, Boolean resizeable,
    JMFrameCallbacks* callbacks)
{
    WindowPtr win;
    Point zeroPt = { 0, 0 };
    
    /* callbacks with pointers to your implementation-- */
    /* note that you also fill in the version number that you */
    /* compiled against (based on what you passed to JMOpenSession) */

    callbacks->fVersion = kJMVersion;
    callbacks->fSetFrameSize = MyResizeRequest;
    callbacks->fInvalRect = MyInvalRect;
    callbacks->fShowHide = MyShowHide;
    callbacks->fSetTitle = MySetTitle;
    callbacks->fCheckUpdate = MyCheckUpdate;
    callbacks->fReorderFrame = MyFrameReorder
    callbacks->fSetResizeable = MySetResizeable
        
    win = NewCWindow(nil, &bounds, "\p", false, documentProc,
        (WindowPtr) -1, true, (long) newFrame);
    if (win == nil)
        return memFullErr;

    JMSetFrameVisibility(newFrame, win, zeroPt, nil);

    return JMSetFrameData(newFrame, (JMClientData) win);
}

The MyRequestFrame function in this example calls the Mac OS Toolbox function NewCWindow to request a new window that corresponds to the frame. If you prefer, you can use an existing window instead. This example always creates a window of type documentProc (a simple document window without size or zoom boxes), but you can select different window types depending on the kind parameter passed into the callback function. See Frame Types for a listing of possible requests.

After creating the window, the JMSetFrameVisibility function JMSetFrameVisibility registers the window characteristics (the graphics port, its position, and its clipping region) with the frame. Whenever the visibility of the window changes (for example, due to scrolling), you must call JMSetFrameVisibility again to update the visibility information.

The function MyRequestFrame also requires a number of callback functions that allow the Java program to manipulate the new window (for example, to show, hide, or update the window). For more information on these functions, see Displaying Frames and Application-Defined Functions.

As part of the NewCWindow call, the reference to the frame (as held in newFrame ) is stored in the refCon field of the window record (a WindowRecord structure). Doing so allows you to determine the frame associated with a window by simply calling the Mac OS Toolbox function GetWRefCon .

In a similar fashion, the JMSetFrameData function is used to store a pointer to the new window record in the frame's client data. You can then easily determine the window associated with a given frame by using a function such as that in Listing 1-9.

Listing 1-9 Determining the window associated with a frame

WindowPtr getFrameWindow(JMFrameRef frame)
{
    if (frame) {
        WindowPtr win = nil;
        if (JMGetFrameData(frame, (JMClientData*) &win) == noErr)
            return win;
    }
    return nil;
}


© 1998 Apple Computer, Inc. — (Last Updated 3 Dec 98)

Previous | Back Up One Level | Next |